﻿using D.B.Data.API.Applications.Command.LeaveCommand;
using D.B.Data.Common;
using D.B.Data.Domain;
using D.B.Data.Domain.DTO.Output;
using D.B.Data.Domain.Enums;
using D.B.Data.ErrorCode;
using D.B.Data.Infrastructuse;
using MediatR;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.StaticFiles;
using NPOI.XSSF.UserModel;
using static D.B.Data.API.Applications.CommandHandler.LeaveCommandHandler.LeaveTableCommandHandler;

namespace D.B.Data.API.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    [Authorize]
    public class LeaveController : ControllerBase
    {
        private readonly IMediator mediator;

        public LeaveController(IMediator mediator)
        {
            this.mediator = mediator;
        }


        /// <summary>
        /// 查询申请
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpGet]
        public Task<APIPaging<QueryLeaveDTO>> QueryLeave([FromQuery]QueryLeaveCommand command)
        {
            return mediator.Send(command);
        }


        /// <summary>
        /// 添加申请
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public Task<APIResult<int>> AddLeave(AddLeaveCommand command)
        {
            return mediator.Send(command);
        }


        /// <summary>
        /// 获取部门Id Name
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpGet]
        public Task<APIResult<List<GetDeptIdNameOutput>>> GetDept([FromQuery]QuerDeptCommand command)
        {
           
            return mediator.Send(command);
        }

        [HttpGet]
        public IActionResult GetEnumType()
        {
            var list = EnumHelp.GetEnum<WorkflowTypeEnums>();
            return Ok(list);
        }

        /// <summary>
        /// 获取部门名称
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpGet]
        public Task<APIResult<string>> GetDeptName([FromQuery]QueryDeptBoosCommand command)
        {
            return mediator.Send(command);
        }

        /// <summary>
        ///删除申请
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public Task<APIResult<int>> Handle([FromQuery]DelLeaveCommand command)
        {
            return mediator.Send(command);
        }

        /// <summary>
        /// 审批
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public Task<APIResult<int>> BoosUp(DeptBoosUpStateCommand command)
        {
            return mediator.Send(command);
        }


        /// <summary>
        /// 修改申请内容
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public Task<APIResult<int>> UpDataLeave(UpdateLeaveCommand command)
        {
            return mediator.Send(command);
        }

        /// <summary>
        /// 获取报表信息
        /// </summary>
        /// <param name=""></param>
        /// <returns></returns>
        [HttpGet]

        public Task<APIPaging<UserLeaveSummaryDto>> GetLeaveTable(int? year, string? userName)
        {
           LeaveTableCommand command=new LeaveTableCommand();
            command.year = year;
            command.UserName = userName;
            return mediator.Send(command);
        }

        /// <summary>
        /// 导出年度请假报表
        /// </summary>
        /// <param name="year"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult ExportData(int? year, string? userName)
        {
            year = year ?? DateTime.Now.Year;
            var res=GetLeaveTable(year, userName).Result.PageData;

            //创建表
            var work = new XSSFWorkbook();

            //创建工作簿
            var sheet=work.CreateSheet($"{year}年度用户申请总工时报表");

            //创建行
            var  row1=sheet.CreateRow(0);

            //创建第一行对应的每一列
            row1.CreateCell(0).SetCellValue("用户名称");
            row1.CreateCell(1).SetCellValue("一月");
            row1.CreateCell(2).SetCellValue("二月");
            row1.CreateCell(3).SetCellValue("三月");
            row1.CreateCell(4).SetCellValue("四月");
            row1.CreateCell(5).SetCellValue("五月");
            row1.CreateCell(6).SetCellValue("六月");
            row1.CreateCell(7).SetCellValue("七月");
            row1.CreateCell(8).SetCellValue("八月");
            row1.CreateCell(9).SetCellValue("九月");
            row1.CreateCell(10).SetCellValue("十月");
            row1.CreateCell(11).SetCellValue("十一月");
            row1.CreateCell(12).SetCellValue("十二月");

            int Index = 1;
            foreach(var item in res)
            {
                //创建行
                var row2 = sheet.CreateRow(Index);

                //创建第一行对应的每一列
                row2.CreateCell(0).SetCellValue(item.UserName);
                int Index1 = 1;
                foreach (var item1 in item.MonthlySummaries)
                {
                    
                    row2.CreateCell(Index1).SetCellValue(item1.TotalLeaveHours);
                    Index1++;
                }
                Index++;
            }

            byte[] data;
            using (var ms=new MemoryStream())
            {
                work.Write(ms);
                data= ms.ToArray();

            }

            string exce = new FileExtensionContentTypeProvider().Mappings[".xlsx"];

                return File(data, exce, $"{year}年度用户申请总工时报表.xlsx");

        }



        /// <summary>
        /// 获取用户请假信息
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpGet]
        public Task<APIPaging<QueryUserLeaveDTO>> GetOneUserLeave([FromQuery]GetOneUserLeaveCommand command)
        {
            return mediator.Send(command);
        }
    }
}
